package cn.fzkj.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import cn.fzkj.common.exception.ErrorsEnum;
import cn.fzkj.gulimall.member.vo.base.SocialUser;
import cn.fzkj.gulimall.member.exception.PhoneExistException;
import cn.fzkj.gulimall.member.exception.UsernameExistException;
import cn.fzkj.gulimall.member.feign.CouponFeignService;
import cn.fzkj.gulimall.member.vo.MemberLoginVo;
import cn.fzkj.gulimall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.fzkj.gulimall.member.entity.MemberEntity;
import cn.fzkj.gulimall.member.service.MemberService;
import cn.fzkj.common.utils.PageUtils;
import cn.fzkj.common.utils.R;



/**
 * 会员
 *
 * @author chenxin
 * @email chenxin@gmail.com
 * @date 2022-02-18 19:11:05
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 远程调用优惠券系统
     */
    @RequestMapping("/coupon")
    public R coupon(){
        MemberEntity member = new MemberEntity();
        member.setNickname("张三");
        R coupons = couponFeignService.member();
        return R.ok().put("member", member).put("coupons", coupons.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public R register(@RequestBody MemberRegisterVo member){
        try {
            memberService.register(member);
        } catch (UsernameExistException e) {
            return R.error(ErrorsEnum.USERNAME_EXIST);
        } catch (PhoneExistException e){
            return R.error(ErrorsEnum.PHONE_EXIST);
        }
        return R.ok();
    }

    @RequestMapping(value = "/oauth/login", method = RequestMethod.POST)
    public R oauthLogin(@RequestBody SocialUser user){
        MemberEntity entity = memberService.login(user);
        if (entity != null){
            return R.ok().data(entity);
        }
        return R.error(ErrorsEnum.USERNAME_PASSWORD_ERROR);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity = memberService.login(vo);
        if (entity != null){
            return R.ok().data(entity);
        }
        return R.error(ErrorsEnum.USERNAME_PASSWORD_ERROR);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
